package com.ruoyi.messages.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.messages.mapper.SysBalanceWarningMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.messages.domain.SysBalanceWarning;
import com.ruoyi.messages.service.ISysBalanceWarningService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息设置Controller
 *
 * @author ruoyi
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/messages/balanceWarning")
public class SysBalanceWarningController extends BaseController
{
    @Autowired
    private ISysBalanceWarningService sysBalanceWarningService;

    @Autowired
    private SysBalanceWarningMapper balanceWarningMapper;

    /**
     * 查询消息设置列表
     */
    @PreAuthorize("@ss.hasPermi('messages:balanceWarning:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBalanceWarning sysBalanceWarning)
    {
        sysBalanceWarning.setType("1");
        sysBalanceWarning.setDeptId(SecurityUtils.getDeptId());
        sysBalanceWarning.setStatus("0");
        startPage();
        List<SysBalanceWarning> list = sysBalanceWarningService.selectSysBalanceWarningList(sysBalanceWarning);
        return getDataTable(list);
    }

    /**
     * 导出消息设置列表
     */
    @PreAuthorize("@ss.hasPermi('messages:balanceWarning:export')")
    @Log(title = "消息设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBalanceWarning sysBalanceWarning)
    {
        List<SysBalanceWarning> list = sysBalanceWarningService.selectSysBalanceWarningList(sysBalanceWarning);
        ExcelUtil<SysBalanceWarning> util = new ExcelUtil<SysBalanceWarning>(SysBalanceWarning.class);
        util.exportExcel(response, list, "消息设置数据");
    }

    /**
     * 获取消息设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('messages:balanceWarning:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysBalanceWarningService.selectSysBalanceWarningById(id));
    }

    /**
     * 新增消息设置
     */
//    @PreAuthorize("@ss.hasPermi('messages:balanceWarning:add')")
    @Anonymous
    @Log(title = "消息设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBalanceWarning sysBalanceWarning)
    {

        return toAjax(sysBalanceWarningService.insertSysBalanceWarning(sysBalanceWarning));
    }

    /**
     * 修改消息设置
     */
    @PreAuthorize("@ss.hasPermi('messages:balanceWarning:edit')")
    @Log(title = "消息设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBalanceWarning sysBalanceWarning)
    {
        Long deptId = SecurityUtils.getDeptId();
        sysBalanceWarning.setDeptId(deptId);

        //先进行查询，如果为空则进行插入，如果不为空则修改
        List<SysBalanceWarning> list = sysBalanceWarningService.selectSysBalanceWarningByDeptId(deptId);

        if (CollectionUtils.isEmpty(list)) {
            //插入
            sysBalanceWarningService.insertSysBalanceWarning(sysBalanceWarning);
        }

        else {
            balanceWarningMapper.updateValueByDeptId(sysBalanceWarning);
        }

        return success("设置成功");
    }

    /**
     * 删除消息设置
     */
    @PreAuthorize("@ss.hasPermi('messages:balanceWarning:remove')")
    @Log(title = "消息设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysBalanceWarningService.deleteSysBalanceWarningByIds(ids));
    }
}
